package com.cool.controller.bigclasscontroller;

import com.cool.bo.PageBo;
import com.cool.bo.bigclassbo.*;
import com.cool.common.CommonResult;
import com.cool.common.ResponseCode;
import com.cool.po.bigclasspo.BigClassPo;
import com.cool.service.bigclass.BigClassService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * 商品大类的控制层
 * 负责人:曹老板
 * 时间:2020/7/28
 */
@RestController
public class BigClassController  {
    //注入业务层对象
    @Autowired
    private BigClassService bigClassService;
    /**
     * 处理根据大类名称模糊查询的请求
     */
    @RequestMapping("selectLike")
    CommonResult selectLike(@RequestBody InputBo inputBo){
        List<BigClassPo> list = bigClassService.selectLike(inputBo);
        System.out.println(list);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("success").setData(list);
    }
    /**
     * 查询商品大类表中的所有数据
     */
    @RequestMapping("getbigClass")
    CommonResult getbigClass(@RequestBody PageBo pageBo){
        System.out.println(pageBo.getXx());
        PageInfo<BigClassPo> list = bigClassService.getbigClass(pageBo);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("success").setData(list);
    }

    /**
     * 文件的上传+新增商品大类
     */
    @RequestMapping("uploadFile")
    CommonResult uploadFile(MultipartFile image, HttpServletRequest request,@RequestBody AddPo addPo) throws IOException {
        //文件的上传
        System.out.println(addPo);
        if(image!=null){
            //取出文件名称
            String fileAllName=image.getOriginalFilename();
            String suffix=fileAllName.substring(fileAllName.lastIndexOf("."));
            //生成随机的文件名称
            String fileName= UUID.randomUUID().toString().replace("-","")+suffix;
            //文件的保存位置
            String realPath = request.getServletContext().getRealPath("/uploadImages");
            File dir=new File(realPath);
            if(!dir.exists()){
                dir.mkdirs();
            }
            //创建服务器中的文件对象
            File targetFile=new File(dir,fileName);
            //通过前端传过来的文件对象复制一份
            image.transferTo(targetFile);
        }
        //调用新增数据的方法
        bigClassService.addbigClass(addPo);
        //返回对象
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("新增成功!");
    }

    /**
     * 文件的下载
     */
    @RequestMapping("downloadFile")
    void downloadFile(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //根据传入的文件名路径获取输入流
        String realPath=request.getServletContext().getRealPath("/uploadImages");
        FileInputStream is=new FileInputStream(realPath+"/"+fileName);
        //响应头设置
        response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode(fileName));
        //通过response输入文件信息
        ServletOutputStream os=response.getOutputStream();
        byte[] b=new byte[1024];
        int len=0;
        while((len=is.read(b))>0){
            os.write(b,0,len);
        }
        is.close();
        os.close();
    }

    /**
     * 多选框批量删除数据
     */
    @RequestMapping("deleteMore")
    CommonResult deleteMore(@RequestBody Checks checks){
        System.out.println(checks.getChecks());
        final List<Object> list = Arrays.asList(checks.getChecks());
        bigClassService.deleteMore(list);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("删除成功!");
    }

    /**
     * 根据主键非空更新
     */
    @RequestMapping("updatebigClass")
    CommonResult updatebigClass(@RequestBody UpdateBo updateBo){
        bigClassService.updatebigClass(updateBo);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功!");
    }
}
